package com.ogawa.base.adapter;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ogawa.base.R;
import com.ogawa.base.utils.imageloader.ImageConfigImpl;
import com.ogawa.base.utils.imageloader.ImageLoader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class GridImageAdapter extends BaseItemDraggableAdapter<GridImageAdapter.GridImageBean, BaseViewHolder> {
    public int MaxNumber = 9;

    public int getMaxNumber() {
        return MaxNumber;
    }

    public void setMaxNumber(int maxNumber) {
        MaxNumber = maxNumber;
    }

    public GridImageAdapter(int layoutResId, @Nullable List<GridImageBean> data) {
        super(layoutResId, data);
    }

    public GridImageAdapter() {
        super(R.layout.gv_filter_image, new ArrayList());

    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, GridImageBean item) {
        helper.setOnClickListener(R.id.iv_del, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.e("点击删除" + helper.getAdapterPosition());
                remove(helper.getAdapterPosition());
                boolean isHaveEmpty = false;
                for (int i = 0; i < getData().size(); i++) {
                    if ("".equals(getData().get(i).path)) {
                        isHaveEmpty = true;
                    }
                }
                if (getData().size() < getMaxNumber() && !isHaveEmpty) {
                    addData(new GridImageBean("", false));
                }
                if (mGridImageAdapterListener != null) {
                    mGridImageAdapterListener.removeItem(helper.getAdapterPosition());
                }

            }
        });
        helper.setOnClickListener(R.id.fiv, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mGridImageAdapterListener != null) {
                    mGridImageAdapterListener.onclickItem(helper.getAdapterPosition());
                }

            }
        });
        ImageView imageView = helper.getView(R.id.fiv);
        if (StringUtils.isEmpty(item.path)) {
            helper.setVisible(R.id.iv_del, false);
            imageView.setImageResource(R.drawable.img_empty_photo);
        } else {
            helper.setVisible(R.id.iv_del, true);

            ImageLoader.getInstance().loadImage(mContext, ImageConfigImpl.builder()
                    .isCenterCrop(true)
                    .url(item.path)
                    .imageView(imageView)
                    .build()
            );


        }
    }

    public static class GridImageBean {
        int reIndex = 0;
        String path;
        boolean isLocalFile;
        File file;

        public File getFile() {
            return file;
        }

        public void setFile(File file) {
            this.file = file;
        }

        public int getReIndex() {
            return reIndex;
        }

        public void setReIndex(int reIndex) {
            this.reIndex = reIndex;
        }

        public GridImageBean(String path, boolean isLocalFile) {
            this.path = path;
            this.isLocalFile = isLocalFile;
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        public boolean isLocalFile() {
            return isLocalFile;
        }

        public void setLocalFile(boolean localFile) {
            isLocalFile = localFile;
        }
    }

    GridImageAdapterListener mGridImageAdapterListener;

    public GridImageAdapterListener getmGridImageAdapterListener() {
        return mGridImageAdapterListener;
    }

    public void setmGridImageAdapterListener(GridImageAdapterListener mGridImageAdapterListener) {
        this.mGridImageAdapterListener = mGridImageAdapterListener;
    }

    public interface GridImageAdapterListener {
        void onclickItem(int position);

        void removeItem(int position);
    }

}


